<template>
  <BasicModal :show="show" title="编辑" width="30%" :widthComfirm="true" @comfirm="comfirm"
              @closeModal="show = false">
    <template #content>
      <CommonForm labelWidth="110px" :formList="formList" ref="formRef"></CommonForm>
    </template>
  </BasicModal>
</template>

<script setup lang='ts'>
import { ref } from "vue"
import CommonForm from "@/views/ApplicationDetails/CommonForm.vue";
import BasicModal from "@/views/ApplicationDetails/Modals/BasicModal.vue"
import {
  updateApplicationTarget,
} from '@/api/data-center/recruitment-progress.ts'
import { message } from "ant-design-vue";

const props = defineProps(['process'])
const emit = defineEmits(['finishEditing'])

// 动态表单配置
const formList = ref([
  {
    label: "分校确认人",
    name: "schoolConfirm",
    value: "",
    component: 'select',
    options: []
  },
  {
    label: "部门确认人",
    name: "deptConfirm",
    value: "",
    component: 'select',
    options: []
  },
  {
    label: "暑假招生目标",
    name: "summerEnrollment",
    value: "",
    component: 'input',
    attrs:{
      type:"number"
    }
  },
  {

    label: "满班率目标",
    name: "fullClassRate",
    value: "",
    component: 'input',
    attrs:{
      type:"number"
    }
  },
  {
    label: "班级限额",
    name: "classQuota",
    value: "",
    component: 'input',
    attrs:{
      type:"number"
    }
  },
  {
    label: "人均带班量",
    name: "classesNum",
    value: "",
    component: 'input',
    attrs:{
      type:"number"
    }
  },
  {
    label: "现有老师",
    name: "teachersNum",
    value: "",
    component: 'input',
    attrs:{
      type:"number"
    }
  },
])

// 弹窗显示状态
const show = ref(false);
// 表单对象ref
const formRef = ref(null)
// 传递过来的表格数据
const itemParams = ref(null)

/**
 * 编辑框打开事件
 * @param params 列表参数
 */
const open = (params) => {
  itemParams.value = params
  params.schoolConfirms.forEach(item=>{
    item.value = item.label
  })
  formList.value[0].options = params.schoolConfirms
  formList.value[1].options = params.schoolConfirms
  formList.value[0].value = params.record.schoolConfirm
  formList.value[1].value = params.record.deptConfirm
  formList.value[2].value = params.record.summerEnrollment
  formList.value[3].value = params.record.fullClassRate
  formList.value[4].value = params.record.classQuota
  formList.value[5].value = params.record.classesNum
  formList.value[6].value = params.record.teachersNum
  show.value = true
}

/**
 * 编辑确认事件
 */
const comfirm = async () => {
  const fromData = await formRef.value.submitForm();
  const params = {
    id: itemParams.value.record.id,
  }
  fromData.forEach(item=>{
    params[item.name] = item.value
  })
  const res = await updateApplicationTarget(params);
  if(res==="成功"){
    message.success("编辑成功");
    show.value = false;
    emit("finishEditing")
  }else{
    message.error(res.msg)
  }
}

defineExpose({ open })
</script>

